# -*- encoding:utf-8 -*-
"""
汉语拼音方案
http://baike.baidu.com/view/104414.htm
w作为声母的，把u省略了
y作为声母，如果后面接u，说明是u..，是变音的。
jqx作为声母，后面接u，说明是u..，但是与nl相接，还是v
iou,uen,uei前面加声母，写成iu,un,ui
"""

shengmulist = ['','b','p','m','f','d','t','n','l','g','k','h','j','q','x','zh','ch','sh','r','z','c','s']
# yumulist seem to lost some 36,3 missing
# a total of 36 yunmu, or 39 yunmu?
yunmulist = ['a','ai','an','ang','ao',
            'e','ei','en','eng','er',
            'o','ong','ou',
            'i','ia','ian','iang','iao',
            'ie','in','ing',
            'iong','iou',
            'u','ua','uai','uan','uang',
            'uei','uen','ueng','uo',
            'v','van','ve','vn']

class PinyinException(Exception):pass
class ShengmuPinyinException(PinyinException):pass
class YunmuPinyinException(PinyinException):pass
class DiaoPinyinException(PinyinException):pass

def splitpinyin(s):
    """
    split the pinyin into shengmu and yunmu
    here should notice the pheonomenon of y and w,which is another form of zero shengmu
    """
    dic = {}
    if not s[len(s)-1].isdigit():
        raise DiaoPinyinException(s+"diao error ")

    dic['diao'] = int(s[len(s)-1])
    

    yunstart = getyunmustart(s)
    dic['shengmu'] = s[0:yunstart]
    dic['yunmu'] = s[yunstart:len(s)-1]
    #print dic['shengmu']
    #print dic['yunmu']

    #特殊情况
    #iou,uen,uei前面加声母，写成iu,un,ui
    changedic = {'iu':'iou','un':'uen','ui':'uei'}
    for k in changedic.keys():
        if dic['yunmu'] == k:
            dic['yunmu'] = changedic[k]
            return dic
    
    if dic['shengmu'] == 'y':
        #change to i according to the situation
        l1 = ['i','a','e','ao','ou','an','in','ang','ing','ong']
        l2 = ['u','ue','uan','un']
        dic['shengmu'] = ""
        if l1.count(dic['yunmu']) == 1:
            if not dic['yunmu'].startswith('i'):
                dic['yunmu'] = 'i'+dic['yunmu']
        elif l2.count(dic['yunmu']) == 1:
            dic['yunmu'] = 'v' + dic['yunmu'][1:len(dic['yunmu'])]
        #else:
            #do noting
            #raise YunmuPinyinException("exception when shengmu is y")
    elif dic['shengmu'] == 'w':
        dic['shengmu'] = ""
        if not dic['yunmu'].startswith('u'):
            dic['yunmu'] = "u" + dic['yunmu']
    elif (dic['shengmu'] == 'j' or dic['shengmu'] == 'q' or dic['shengmu'] == 'x'):
        if dic['yunmu'].startswith('u'):
            dic['yunmu'] = 'v' + dic['yunmu'][1:len(dic['yunmu'])]
    #else:
        #normal part

    if shengmulist.count(dic['shengmu']) != 1:
        raise ShengmuPinyinException(dic['shengmu'])
    if yunmulist.count(dic['yunmu']) != 1:
        raise YunmuPinyinException(dic['yunmu'])

    return dic

def getyunmustart(s):
    """
    get the position where the yunmu starts with
    """
    yunstart = ['a','e','o','i','u','v']
    minindex = 20
    for y in yunstart:
        curposition = 20
        try:
            curposition = s.index(y)
            if minindex > curposition:
                minindex = curposition
        except:
            #do nothing if not found does not return -1 but raise a exception
            i=1
    return minindex

if __name__ == "__main__":
    print splitpinyin("zheng5")
    print getyunmustart("zheng5")
    print splitpinyin("que1")

